package wnhz.com.business.bindphone;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.uitls.ConfirmUtils;
import wnhz.com.business.uitls.citychose.CityCodeChoseActivity;


/**
 * 更棒手机号_新手机号输入
 */
public class BindPhone3Activity extends BaseActivity{

    @BindView(R.id.tv_countryname)//国家名字
    TextView tvCountryname;
    @BindView(R.id.tv_bindphone_countrytel)//国家区号
    TextView tvBindphoneCountrytel;
    @BindView(R.id.ed_bindphone_tel)//输入框_电话号码
    EditText edBindphoneTel;
    @BindView(R.id.btn_bindphone1_next)//按钮_完成
    Button btnBindphone1Next;
    private String code = "";
    private String countryname = "";
    private int RESULT_GETCITYCODE = 200;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bound_phone3);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        //根据输入框内容来设置登录按钮的背景
        edBindphoneTel.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(edBindphoneTel.getText().toString())) {
                    if (count == 0) {
                        btnBindphone1Next.setBackground(getResources().getDrawable(R.drawable.bg_circle_grayd));
                        btnBindphone1Next.setEnabled(false);
                        btnBindphone1Next.setClickable(false);
                    } else {
                        btnBindphone1Next.setBackground(getResources().getDrawable(R.drawable.bg_circle_reds));
                        btnBindphone1Next.setEnabled(true);
                        btnBindphone1Next.setClickable(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick({R.id.rl_register_back,R.id.rl_chose_country,R.id.btn_bindphone1_next})
    void Click(View v){
        Intent intent = null;
        switch (v.getId()){
            case R.id.rl_register_back:
                finish();
                break;
            case R.id.rl_chose_country://选择国家
                intent = new Intent(BindPhone3Activity.this, CityCodeChoseActivity.class);
                startActivityForResult(intent, RESULT_GETCITYCODE);
                break;
            case R.id.btn_bindphone1_next://下一步
                if (TextUtils.isEmpty(edBindphoneTel.getText().toString())){
                    MyToast("请输入新的手机号码");
                    return;
                }
                if (!ConfirmUtils.isMobile(edBindphoneTel.getText().toString())){
                    MyToast("手机号码格式不正确");
                    return;
                }
                intent = new Intent(BindPhone3Activity.this,BindPhone4Activtity.class);
                intent.putExtra("phone",edBindphoneTel.getText().toString());
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_GETCITYCODE && resultCode == 0) {
            if (data != null) {
                /**
                 * 获取国家地区的电话区号和国家名称
                 */
                code = data.getStringExtra("code");
                countryname = data.getStringExtra("area");
                tvCountryname.setText(countryname);
                tvBindphoneCountrytel.setText(code);
            }
        }
    }
}
